package com.safety.apitest.transform;

import com.safety.apitest.beans.SensorReading;
import org.apache.flink.streaming.api.collector.selector.OutputSelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SplitStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Collections;

/**
 * @Author: QingW
 * @Date: 2021/9/18
 */
public class TransformTest4_ConnectStream {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> dataStream = env.readTextFile("E:\\flink\\flink-learning\\src\\main\\resources\\sensor.txt");
        DataStream<SensorReading> sensorStream = dataStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });

//        分流
        SplitStream<SensorReading> readingSplitStream = sensorStream.split(new OutputSelector<SensorReading>() {
            @Override
            public Iterable<String> select(SensorReading value) {
                return (value.getTemperature() > 30) ? Collections.singletonList("high") : Collections.singletonList("low");
            }
        });

        DataStream<SensorReading> hight = readingSplitStream.select("high");
        DataStream<SensorReading> low = readingSplitStream.select("low");
        DataStream<SensorReading> all = readingSplitStream.select("high", "low");
        hight.print("hight");
        low.print("low");
        all.print("all");

        env.execute("Wang-Create-JobFile");

    }


}
